home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ldb.zip / SDATA.CPP < prev    next >
C/C++ Source or Header  |  1991-10-18  |  2KB  |  122 lines

  1. /*
  2.  
  3.     sdata.cpp
  4.     10-18-91
  5.     Streamable Data: Loose Data Binder v1.4
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.  
  11.     PSW / Power SoftWare
  12.     P.O. Box 10072
  13.     McLean, Virginia 22102 8072 USA
  14.  
  15.     John Small
  16.     Voice: (703) 759-3838
  17.     CIS: 73757,2233
  18.  
  19. */
  20.  
  21. #include "sdata.hpp"
  22. #include <string.h>
  23.  
  24.  
  25. void SData::construct(voiD D, unsigned sizeofData,
  26.     unsigned Did, int dup)
  27. {
  28.     this->Did = Did;
  29.     if ((this->dup = dup) != 0)
  30.         if (sizeofData && D)
  31.             if ((this->D =     new char 
  32.                 [sizeofData]) != voiD0)
  33.                 memcpy(this->D,D,
  34.                     this->sizeofData
  35.                     =sizeofData);
  36.             else  {
  37.                 this->sizeofData = 0;
  38.                 this->dup = 0;
  39.             }
  40.         else  {
  41.             this->sizeofData = 0;
  42.             this->dup = 0;
  43.             this->D = voiD0;
  44.         }
  45.     else if ((this->D = D) == voiD0)
  46.         this->sizeofData = 0;
  47.     else
  48.         this->sizeofData = sizeofData;
  49. }
  50.  
  51. ostream& SData::store(ostream& os)
  52. {
  53.     os << sizeofData << endm << Did << endm;
  54.     if (!os)
  55.         serror("unable to store SData"
  56.             ": sizeofData and Did");
  57.     else if (sizeofData)  {
  58.         os.write((const char *)D,sizeofData);
  59.         if (!os)
  60.             serror("unable to store"
  61.                 " SData data");
  62.     }
  63.     return os;
  64. }
  65.  
  66. StreamablE SData::load(istream& is,
  67.     StreamablE InstancE)
  68. {
  69.     unsigned sizeofData, Did;
  70.     voiD D = voiD0;
  71.  
  72.     if (!(is >> sizeofData >> nextm >> Did
  73.         >> nextm))  {
  74.         lserror("unable to load SData"
  75.             ": sizeofData and Did",
  76.             ID_CLASS);
  77.         return StreamablE0;
  78.     }
  79.     if (sizeofData) {
  80.         if ((D = new char[sizeofData])
  81.             != voiD0)  {
  82.             is.read((char *)D,sizeofData);
  83.             if (!is)  {
  84.                 lserror("loading "
  85.                     "SData data",
  86.                     ID_CLASS);
  87.                   delete D;
  88.                 return StreamablE0;
  89.             }
  90.         }
  91.         else  {
  92.             lserror("unable to allocate "
  93.                 "memory for SData data",
  94.                 ID_CLASS);
  95.             is.ignore(sizeofData);
  96.             return StreamablE0;
  97.         }
  98.     }
  99.     if (!InstancE)
  100.         if ((InstancE =    (StreamablE)
  101.             new SData
  102.             (UNIQUE_STREAMABLE))
  103.             == StreamablE0)  {
  104.             lserror("unable to construct"
  105.                 " SData",ID_CLASS);
  106.             delete D;
  107.             return StreamablE0;
  108.         }
  109.     ((SDatA)InstancE)->
  110.         construct(D,sizeofData,Did,0);
  111.     ((SDatA)InstancE)->dup = 1;
  112.     
  113.     return InstancE;
  114. }
  115.  
  116. SData::SData(char * s, int dup)
  117.     : Streamable(UNIQUE_STREAMABLE,ID_CLASS)
  118. {
  119.     construct(s,(dup?(s?(strlen(s)+1):0):0),
  120.         DID_String,dup);
  121. }
  122.